using System.Collections.Generic;
using com.yoozoo.gta;
using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.StreetEvent;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    public enum SearchType
    {
        SearchItemId,
        SearchItemType,
        SearchItemBindID,
    }
    public class NpcSearchForItems : NavMeshMovement
    {
        public BBParameter<SearchType> SearchItemType;
        public BBParameter<int> ItemId;
        public BBParameter<int> ItemType;
        private List<PropItem> _items;
        private Vector3 m_TargetPosition;
        private bool m_PathFound;
        

        protected override void OnExecute()
        {
            base.OnExecute();
            _items = new List<PropItem>();
            if (SearchItemType.value == SearchType.SearchItemId)
            {
                StreetFightEvent.Instance.GetAllItemTransArrayById(ItemId.value,_items);
            }
            else if (SearchItemType.value == SearchType.SearchItemBindID)
            {
                StreetFightEvent.Instance.GetAllItemTransArrayById(ItemId.value,_items);
            }
            else
            {
                StreetFightEvent.Instance.GetAllItemTransArrayByType(ItemType.value,_items);
            }
            if (_items.Count > 0)
            {
                var maxDistance = float.MaxValue;
                foreach (var item in _items)
                {
                    var distance = Vector3.Distance(item.transform.position, _entity.data.position);
                    if (distance < maxDistance)
                    {
                        m_TargetPosition = item.transform.position;
                        maxDistance = distance;
                    }
                }
                SetDestination(m_TargetPosition);
                m_PathFound = true;
            }
            else
            {
                m_PathFound = false;
                EndAction(false);
            }
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            if (m_PathFound)
            {
                if (HasArrived())
                {
                    EndAction(true);
                }
            }
            else
            {
                EndAction(false);
            }
        }
    }
}